﻿using DCommon.Caching.Redis;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Diagnostics;

namespace DCommon.Caching.Sample
{
    class RedisCacheRunner
    {
        public static void Run()
        {
            var services = new ServiceCollection();
            services.AddSingleton(sp => new RedisService("localhost:32768"));
            services.AddDistributedRedisCache(option => { option.InstanceName = ""; });
            services.AddMemoryCache(option => { });
            services.AddSingleton<ICacheSerializer, JsonCacheSerializer>();
            services.AddSingleton<ICacheProvider, DistributedCacheProvider>();
            services.AddSingleton<CacheFactory>();
            services.AddSingleton<PostManager>();
            var serviceProvider = services.BuildServiceProvider();
            var postManager = serviceProvider.GetRequiredService<PostManager>();
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < 1000; i++)
            {
                var ids = new[] { Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString() };
                postManager.GetPosts(ids);
            }
            stopwatch.Stop();
            Console.WriteLine($"DRedisCacheRunner Elapsed:{stopwatch.Elapsed}");
        }
    }
}
